<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>评论管理</title>
		<link rel="stylesheet" href="../../component/pear/css/pear.css" />
	</head>
	<body class="pear-container">
		<div class="layui-card">
			<div class="layui-card-body">
				<form class="layui-form" action="">
					<div class="layui-form-item">
						<label class="layui-form-label">评论人</label>
						<div class="layui-input-inline">
							<input type="text" name="userName" placeholder="" class="layui-input">
						</div>
                        <label class="layui-form-label">内容</label>
                        <div class="layui-input-inline">
                            <input type="text" name="content" placeholder="" class="layui-input">
                        </div>
                        <label class="layui-form-label">文章</label>
                        <div class="layui-input-inline">
                            <input type="text" name="articleName" placeholder="" class="layui-input">
                        </div>
						<button class="pear-btn pear-btn-md pear-btn-primary query" lay-submit lay-filter="user-query">
							<i class="layui-icon layui-icon-search"></i>
							查询
						</button>
						<button type="reset" class="pear-btn pear-btn-md">
							<i class="layui-icon layui-icon-refresh"></i>
							重置
						</button>
					</div>
				</form>
			</div>
		</div>
		<div class="layui-card">
			<div class="layui-card-body">
				<table id="user-table" lay-filter="user-table"></table>
			</div>
		</div>

	<script type="text/html" id="user-toolbar">
		<!--<button class="pear-btn pear-btn-primary pear-btn-md permission" permission = 'blogComment:add'  lay-event="add" >
			<i class="layui-icon layui-icon-add-1"></i>
			新增
		</button>-->
		<button class="pear-btn pear-btn-danger pear-btn-md permission" permission = 'blogComment:del' lay-event="batchRemove" >
			<i class="layui-icon layui-icon-delete"></i>
			删除
		</button>
	</script>

	<script type="text/html" id="user-bar">
<!--		<button class="pear-btn pear-btn-primary pear-btn-sm permission" permission = 'blogComment:update' lay-event="edit"><i class="layui-icon layui-icon-edit" ></i></button>-->
<!--        <button class="pear-btn pear-btn-warming pear-btn-sm permission" permission = 'blogcategory:power' lay-event="power"><i class="layui-icon layui-icon-vercode"></i></button>-->
		<button class="pear-btn pear-btn-danger pear-btn-sm permission" permission = 'blogComment:del' lay-event="remove"><i class="layui-icon layui-icon-delete" ></i></button>
        {{#  if(d.replyId ===null ){ }}
        <button class="pear-btn pear-btn-primary pear-btn-sm permission" permission = 'blogComment:del' lay-event="reply"><i class="layui-icon layui-icon-reply-fill" >回复</i></button>
        {{#  } }}
<!--        <button class="pear-btn pear-btn-sm" id="more_{{d.userId}}"><i class="layui-icon layui-icon-triangle-d"></i></button>-->
	</script>

	<script type="text/html" id="user-enable">
		<input type="checkbox" title="{{deleteHtmlTags(d.content)}}" name="status" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="user-enable" {{ d.status== 1 ? 'checked' : '' }} />
	</script>

	<script type="text/html" id="user-sex">
		{{#if (d.sex == 1) { }}
		<span>男</span>
		{{# }else if(d.sex == 0){ }}
		<span>女</span>
		{{# } }}
	</script>

	<script type="text/html" id="user-login">
		{{#if (d.login == 0) { }}
		<span>在线</span>
		{{# }else if(d.sex == 1){ }}
		<span>离线</span>
		{{# } }}
	</script>

	<script type="text/html" id="user-createTime">
		{{layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss')}}
	</script>

        <div style="display: none;">
            <div class="layer-top">
                <div class="layui-row layui-col-space10">
                    <div class="layui-col-md12">

                        <form class="layui-form" action="javascript:void(0);" lay-filter="example">
                            <div class="layui-card" style="margin-top: 10px;">
                                <div class="layui-card-header">回复记录</div>
                                <div class="layui-card-body layui-row layui-col-space10">
                                    <div class="layui-col-md12">
                                        <input type="text" name="name" placeholder="请输入用户名" class="layui-input"
                                               lay-verify="required" autocomplete="off">
                                        <input type="text" name="id" placeholder="请输入ID" class="layui-input" style="display: none">
                                    </div>
                                </div>
                                <div class="layui-card-header">标签颜色</div>
                                <div class="layui-card-body layui-row layui-col-space10">
                                    <div class="layui-col-md6">
                                        <input type="text" name="tagColor" hover placeholder="账号"
                                               class="layui-input" autocomplete="off">
                                    </div>
                                </div>
                                <div class="layui-card-header">序号</div>
                                <div class="layui-card-body layui-row layui-col-space10">
                                    <div class="layui-col-md6">
                                        <input name="sort" type="number" placeholder="请填写入序号" value="" class="layui-input" lay-verify="required|number">
                                    </div>

                                </div>
                            </div>
                            <div class="layui-form-item" style="margin-top: 20px;padding-right:50px;float: right">
                                <button type="button" class="pear-btn pear-btn-success login" lay-submit
                                        lay-filter="login">
                                    保 存
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

		<script src="../../component/layui/layui.js"></script>
		<script src="../../component/pear/pear.js"></script>
        <script src="../../js/baseUrl.js"></script>
        <script src="../../js/cookieUtils.js"></script>
        <script src="../../component/pear/module/ajaxRequest.js"></script>
        <script src="../../js/jquery-3.2.1.min.js"></script>
        <script src="../../js/permission.js"></script>
		<script>
			layui.use(['table', 'form', 'jquery', 'drawer', 'dropdown','ajaxRequest','toast'], function() {
				let table = layui.table;
				let form = layui.form;
				let $ = layui.jquery;
				let drawer = layui.drawer;
				let dropdown = layui.dropdown;
                let ajaxRequest = layui.ajaxRequest;
                let toast = layui.toast;

				let MODULE_PATH = "/system/user/";

				let cols = [
					[{
							type: 'checkbox'
						},
						{
							title: '评论人',
							field: 'userName',
							align: 'center',
							width: 110
						},
						{
							title: '评论内容',
							field: 'content',
							align: 'center',
						},
                        {
                            title: '状态',
                            field: 'status',
                            align: 'center',
                            templet: '#user-enable',
                            width: 100
                        },
                        {
                            title: '文章标题',
                            field: 'articleName',
                            align: 'center',
                            width: 150,
                        },
                        {
                            title: '回复评论',
                            field: 'replyId',
                            align: 'center',
                        },
						{
							title: '评论时间',
							field: 'commentDate',
							align: 'center',
                            templet: '#user-createTime',
                            width: 150
						},
						{
							title: '操作',
							toolbar: '#user-bar',
							align: 'left',
							width: 130,
							fixed: 'right'
						}
					]
				]

				var tableName = table.render({
					elem: '#user-table',
					url: baseUrl + "/admin/comment/getBlogCommentList",
                    headers: {'Authorization': tokenPrefix + token},
					page: true,
					cols: cols,
					skin: 'line',
					toolbar: '#user-toolbar',
					defaultToolbar: [{
						layEvent: 'refresh',
						icon: 'layui-icon-refresh',
					}, 'filter', 'print', 'exports'],
					done: function(res, curr, count) {
					    // 添加权限验证
					    validaPermission();
					    // 添加角色验证
					    // validaRole();
                        }
				});

				table.on('tool(user-table)', function(obj) {
					if (obj.event === 'remove') {
						window.remove(obj);
					} else if (obj.event === 'reply') {
						window.reply(obj);
					} else if(obj.event === 'power'){
                        window.power(obj);
                    }
				});

				table.on('toolbar(user-table)', function(obj) {
					if (obj.event === 'add') {
						// window.add();
					} else if (obj.event === 'refresh') {
						window.refresh();
					} else if (obj.event === 'batchRemove') {
						window.batchRemove(obj);
					}
				});

				form.on('submit(user-query)', function(data) {
				    // console.log(data.field)
					table.reload('user-table', {
						where: data.field
					})
					return false;
				});

				form.on('switch(user-enable)', function(obj) {
                    var data = new Object();
                    data.id = this.value;
                    if (obj.elem.checked) {
                        data.status = 1;
                    }else {
                        data.status = 0;
                    }
                    var res = ajaxRequest.sendPost("/admin/comment/updateStatus",data,false,true);
                    if (res) {
                        toast.success({title: 'Success',message: res.msg,position: 'topRight'});
                        layer.tips(this.title + ' ' + this.name + '：' + obj.elem.checked, obj.othis);
                    }
				});

				window.remove = function(obj) {
					layer.confirm('确定要删除该评论吗吗（建议禁用即可）', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();

                        var res = ajaxRequest.sendPost("/admin/comment/delBlogComment",{'id':obj.data['id']},false,true);
                        if (res){
                            layer.close(loading);
                            toast.success({title: 'Success',message: res.msg,position: 'topRight'});
                            $('.query').click();
                        }
					});
				}

                window.reply = function() {
				    layer.msg("改成了去博客里回复！！！")
                    /*drawer.open({
                        direction: "right",
                        dom: ".layer-top",
                        distance: "70%"
                    });
                    form.val('example', {"id":"","name":"","tagColor":"","sort":""});*/
                };

				window.batchRemove = function(obj) {
					let data = table.checkStatus(obj.config.id).data;
					if (data.length === 0) {
						layer.msg("未选中任何数据", {
							icon: 3,
							time: 1000
						});
						return false;
					}
					let ids = "";
					for (let i = 0; i < data.length; i++) {
						ids += data[i].id + ",";
					}
					ids = ids.substr(0, ids.length - 1);
					layer.confirm('确定要删除这些评论吗（建议禁用即可）', {
						icon: 3,
						title: '提示'
					}, function(index) {
						layer.close(index);
						let loading = layer.load();
                        var res = ajaxRequest.sendPost("/admin/comment/delBlogCommentIds",{'ids':ids},false,true);
                        if (res){
                            layer.close(loading);
                            toast.success({title: 'Success',message: res.msg,position: 'topRight'});
                            $('.query').click();
                        }

					});
				}
				window.refresh = function(param) {
					table.reload('user-table');
				}

			})

			// 将字符串中的html标签进行替换
			function deleteHtmlTags(htmlStr){
				//定义script的正则表达式，去除js可以防止注入
				/*var scriptRegex = /<script[^>]*?>[\\s\\S]*?<\\/script>/gm;
                //定义style的正则表达式，去除style样式，防止css代码过多时只截取到css样式代码
                var styleRegex = /<style[^>]*?>[\\s\\S]*?<\\/style>/gm;*/
				//定义HTML标签的正则表达式，去除标签，只提取文字内容
				// 排除所有html标签
				// var htmlRegex = /<[^>]+>/gm;
				var regx = /<[^>]*>|<\/[^>]*>/gm;
				// 排除除了img、p、span标签以外的所有标签
				// var htmlRegex = "(?!<(img|p|span).*?>)<.*?>";
				// var htmlRegex = "(?!<(img).*?>)<.*?>";
				//定义空格,回车,换行符,制表符
				var spaceRegex = /\\s*|\t|\r|\n/gm;
				// 过滤script标签
				// htmlStr = htmlStr.replace(scriptRegex, "");
				// 过滤style标签
				// htmlStr = htmlStr.replace(styleRegex, "");
				// 过滤html标签
				// htmlStr = htmlStr.replace(htmlRegex, "");
				htmlStr = htmlStr.replace(regx, "");
				// 过滤空格等
				htmlStr = htmlStr.replace(spaceRegex, "");
				// 过滤&nbsp;
				htmlStr = htmlStr.replace("&nbsp;", "");
				// 过滤&nbsp
				htmlStr = htmlStr.replace("&nbsp", "");
				// 返回文本字符串
				htmlStr = htmlStr.trim();
				//去除空格" "
				htmlStr = htmlStr.replaceAll(" ", "");

				return htmlStr;
			}

            
		</script>
	</body>
</html>
